import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { Photo } from './photo.entity'

@Injectable()
export class PhotoService {
  constructor(
    @InjectRepository(Photo)
    private readonly photoRepository: Repository<Photo>,
  ) {}

  async findAll(): Promise<Photo[]> {
    return await Photo.find()
  }

  async create(): Promise<string> {
    const photo = new Photo()
    photo.name = 'asc'
    photo.description = 'text'
    photo.filename = 'shanghai'
    photo.isPublished = true
    console.log(photo)

    return photo
      .save()
      .then(res => {
        console.log('succ')
        return 'create employee ...done'
      })
      .catch(err => {
        console.log({ err })
        return err
      })
  }

  async findOne(name: string): Promise<Photo> {
    return await Photo.findOne({ name })
  }
}
